﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using phones.Model;

namespace phones.Repository
{
    public class DepartmentsRepository : BaseRepository<Department>
    {
        public DepartmentsRepository(phonesEntities context) : base(context) { }
        public DepartmentsRepository() : base() { }

        public ObservableCollection<Department> GetList(ServiceNode node, Organisation org)
        {

            if (node == null)  return null;
            if (org == null) return null;
            
            ObservableCollection<Department> list = new ObservableCollection<Department>();
            var queryorg = from d in Context.Departments
                            join c in Context.Contacts on d.ID equals c.Department.ID
                            join o in Context.Organisations on d.Organisation.ID equals o.ID
                            where c.City.ServiceNode.ID == node.ID && o.ID == org.ID
                            orderby d.Name ascending
                            select d;

            list = new ObservableCollection<Department>(queryorg.Distinct().ToList());
            return list;

        }
    }
}
